package com.waiting.leetcode;

import java.util.LinkedList;
import java.util.List;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/5
 **/
public class ArrayOP {
    public static void main(String[] args) {
        ArrayOP test = new ArrayOP();
        int target = 11;
        int[] nums = {2, 3, 6};
        test.ite(nums, target);
    }

    public void ite(int[] nums, int target) {
        if (nums == null) {
            return;
        } else if (nums.length == 1) {
            if (nums[0] == target) {
                System.out.println(nums[0]);
                return;
            } else {
                return;
            }
        } else {
            List<String> tmp = new LinkedList<>();
            tmp.add(nums[0] + "");
            test(nums, 1, target - nums[0], tmp);
        }
    }

    /**
     * 迭代
     *
     * @param nums
     * @param i
     * @param target
     * @param one
     */
    public void test(int[] nums, int i, int target, List<String> one) {
        if (i > nums.length - 1) {
            if (target == 0) {
                print(one);
            }
            return;
        }
        int length = one.size();
        one.add("+");
        one.add(nums[i] + "");
        test(nums, i + 1, target - nums[i], one);
        //将数据从list中移除
        one.remove(length + 1);
        one.remove(length );

        one.add("-");
        one.add(nums[i] + "");
        test(nums, i + 1, target + nums[i], one);
    }

    public void print(List<String> tmp) {
        StringBuilder stringBuilder = new StringBuilder(tmp.toString());
        System.out.println(stringBuilder);
    }
}
